package cashpointer.core;

import java.util.Map;
import java.util.LinkedHashMap;
import java.util.HashSet;
import java.util.Set;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
import java.util.Collections;

/**
 * PROBLEMY
 * - cos nie tak z tranzakcyjnoscia, przetestowac!!!
 * http://static.springframework.org/spring/docs/2.0.0/reference/transaction.html
 * http://forum.springsource.org/showthread.php?t=16884
 * http://www.google.pl/search?q=+Not+closing+pre-bound+Hibernate+Session+after+HibernateTemplate&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:pl-PL:unofficial&client=firefox-a
 */

public class Main {
    // Akcje
    private static Set<String> ACTIONS = new HashSet<String>();
    static {
        ACTIONS.add("init");
        ACTIONS.add("dbtest");
        ACTIONS.add("nettest");
        ACTIONS.add("update");
        ACTIONS.add("version");
        ACTIONS.add("help");

        ACTIONS = Collections.unmodifiableSet(ACTIONS);
    }

    public static Map<String, String> VALUES = new LinkedHashMap<String, String>();
    static {
        VALUES.put("msg", "");
        VALUES.put("isOut", "no");

		VALUES = Collections.unmodifiableMap(VALUES);
    }

    public static void main(String[] args) throws Exception {
        // Odczytywanie argumentu
        String actionName = null;
        if (args.length > 0 && args.length == 1) {
            actionName = args[0];
        }

        // Jesli nazwa akcji nie znajduje sie na liscie
        if (!ACTIONS.contains(actionName)) {
            actionName = new String("help");
        }

        ConfigurationManager confManager = new ConfigurationManager();
        confManager.addConfigurationProvider(new XmlConfigurationProvider("xwork.xml", true));

        Configuration conf = confManager.getConfiguration();
        ActionProxy actionProxy = null;
        if (conf != null) {
            ActionProxyFactory actionProxyFactory = (ActionProxyFactory) conf.getContainer().getInstance(ActionProxyFactory.class);

            if (actionProxyFactory != null) {
                actionProxy = actionProxyFactory.createActionProxy("/core", actionName, VALUES);
            }
        }

        if (actionProxy != null) {
            actionProxy.execute();
        }
    }
}